import { NodePool, Prefab, Node, Vec3, instantiate } from "cc";

/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * http://www.fenglinghudong.com/
 * 
 * 节点池 管理类
 * chengen
 * 2023-08-24 11:52:35
 */

export class PoolManager {
    /** 节点池 单例 **/
    private static _instance: PoolManager;

    public _dictPool: any = {}
    public _dictPrefab: any = {}

    static get instance() {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new PoolManager();
        return this._instance;
    }

    public getNode(prefab: Prefab | string, parent?: Node, pos?: Vec3): Node {
        let tempPre;
        let name;
        if (typeof prefab === 'string') {
            tempPre = this._dictPrefab[prefab];
            name = prefab;
            if (!tempPre) {
                console.log("Pool invalid prefab name = ", name);
                return null;
            }
        }
        else {
            tempPre = prefab;
            name = prefab.data.name;
        }

        // console.log(this._dictPool);

        let node = null;
        if (this._dictPool.hasOwnProperty(name)) {
            //已有对应的对象池
            let pool = this._dictPool[name];
            if (pool.size() > 0) {
                node = pool.get();
            } else {
                node = instantiate(tempPre);
            }
        } else {
            // 没有对应对象池，创建他！
            let pool = new NodePool();
            this._dictPool[name] = pool;

            node = instantiate(tempPre);
        }

        if (parent) {
            node.parent = parent;
            node.active = true;
            if (pos) node.position = pos;
        }

        return node;
    }

    public setPrefab(name: string, prefab: Prefab): void {
        this._dictPrefab[name] = prefab;
    }

    public clearPool(name: string) {
        if (this._dictPool.hasOwnProperty(name)) {
            let pool = this._dictPool[name];
            pool.clear();
        }
    }

    /**获取缓存池中的数量 */
    public getSize(prefab: Prefab | string) {
        let tempPre;
        let name;
        let size = 0;
        if (typeof prefab === 'string') {
            tempPre = this._dictPrefab[prefab];
            name = prefab;
            if (!tempPre) {
                return -1;
            }
        }
        else {
            name = prefab.data.name;
        }

        if (this._dictPool.hasOwnProperty(name)) {
            //已有对应的对象池
            let pool = this._dictPool[name];
            size = pool.size();
        }
        return size;
    }

    public putNode(node: Node | null, isActive = false) {
        if (!node) {
            return;
        }

        let name = node.name;
        let pool = null;
        // node.active = isActive
        if (this._dictPool.hasOwnProperty(name)) {
            // 已有对应的对象池
            pool = this._dictPool[name];
        } else {
            //没有对应对象池，创建他！
            pool = new NodePool();
            this._dictPool[name] = pool;
        }

        pool.put(node);
    }

    public getPrefab(name: string): Prefab {
        return this._dictPrefab[name];
    }
}
(window as any).PoolManager = PoolManager;